//
//  ItemListController.m
//  Stock
//
//  Created by blacksun on 11-6-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ItemListController.h"


@implementation ItemListController
@synthesize scrollView;
@synthesize childViewController;
@synthesize lastChildView;
@synthesize leftOffsetImgaeView;
@synthesize rightOffsetImageView;
@synthesize selectedImageView;
@synthesize stockSection;
@synthesize titleView;
@synthesize updateTimeLabel;
@synthesize containerView;
@synthesize bannerView;


-(void)didClikedStockChildSegment:(id)sender{
    UIButton *button = (UIButton *)sender;
    if (button.tag == selectedChildIndex) {
        return;
    }
    selectedChildIndex = button.tag;
    
    [self.lastChildView removeFromSuperview];
    self.lastChildView = nil;
    self.childViewController = nil;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelay:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    self.selectedImageView.frame = CGRectMake(selectedChildIndex*100.0+10.0, 5.0, 100.0, 30.0);
    [UIView commitAnimations];
    
    NSArray *stockChildren = [stockSection objectForKey:kStockChilren];
    NSDictionary *stockChildDict = [stockChildren objectAtIndex:button.tag];
    Class clazz = NSClassFromString([stockChildDict objectForKey:kStockChildController]);
    NSString *xibName = [stockChildDict objectForKey:kStockChildXib];
    UIViewController *childController;
    if ([xibName length] == 0) {
        childController = [[[clazz alloc] initWithStyle:UITableViewStylePlain] autorelease];
        NSString *stockChildId = [stockChildDict objectForKey:kStockChildId];
        NormalStockChildListController * temp = (NormalStockChildListController *)childController;
        temp.sectionId = stockChildId;
        temp.sectionIndex = [[StockContext shareInstance] findMaxIndexWithSectionId:stockChildId];
        
    }else{
        childController = [[[clazz alloc] initWithNibName:xibName bundle:nil] autorelease];
        if ([childController respondsToSelector:@selector(setKey:)]) {
            [childController performSelector:@selector(setKey:) withObject:[stockChildDict objectForKey:kStockChildId]];
        }
    }
    [self.containerView addSubview:childController.view];
    self.lastChildView = childController.view;
    self.childViewController = childController;
    if (stockChildren && [stockChildren count]>1) {
        childController.view.frame = CGRectMake(0.0, 40.0, 320.0, 376.0);
    }else{
        childController.view.frame = CGRectMake(0.0, 0.0, 320.0, 416.0);
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView{
    float offsetX = aScrollView.contentOffset.x;
    if (offsetX > 5.0) {
        leftOffsetImgaeView.hidden = NO;
    }else{
        leftOffsetImgaeView.hidden = YES;
    }
    NSArray *stockChildren = [stockSection objectForKey:kStockChilren];
    if (offsetX>[stockChildren count]*100.0-330.0) {
        rightOffsetImageView.hidden = YES;
    }else{
        rightOffsetImageView.hidden = NO;
    }
}

- (void)dealloc
{
    self.scrollView = nil;
    self.childViewController = nil;
    self.lastChildView = nil;
    self.leftOffsetImgaeView = nil;
    self.rightOffsetImageView = nil;
    self.selectedImageView = nil;
    self.stockSection = nil;
    self.titleView = nil;
    self.updateTimeLabel = nil;
    self.containerView = nil;
    self.bannerView.delegate=nil;
    self.bannerView = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    selectedChildIndex = UINT_MAX;
    
    self.scrollView.delegate = self;
    StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSDictionary *stockDict = [delegate.stockSections objectAtIndex:[[StockContext shareInstance] workingStock]];
    self.stockSection = stockDict;
    NSArray *children = [stockSection objectForKey:kStockChilren];
    if (children && [children count]>1) {
        for (int i=0; i<[children count];i++) {
            NSDictionary *stockChild = [children objectAtIndex:i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(100.0*i+10.0, 0.0, 100.0, 40.0);
            [button setTitle:[stockChild objectForKey:kStockChildName] forState:UIControlStateNormal];
            button.tag = i;
            [button addTarget:self action:@selector(didClikedStockChildSegment:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
        }
        [self.scrollView setContentSize:CGSizeMake([children count]*100.0+20.0, 40.0)];
    }else{
        self.titleView.hidden = YES;
    }
    self.leftOffsetImgaeView.hidden = YES;
    if ([children count]<4) {
        self.rightOffsetImageView.hidden = YES;
    }
    
    [self didClikedStockChildSegment:nil];
    
    if ([StockContext shareInstance].workingStock == 1 && [StockContext shareInstance].updateTimes) {
        self.containerView.frame = CGRectMake(0.0, 30.0, 320.0, 416.0);
        self.updateTimeLabel.hidden = NO;
        NSString *pvdt = @"";
        for (NSDictionary *dict in [StockContext shareInstance].updateTimes) {
            if ([@"PVDT" isEqualToString:[dict objectForKey:@"id"]]) {
                pvdt = [dict objectForKey:@"time"];
            }
        }
        updateTimeLabel.text = [updateTimeLabel.text stringByAppendingString:pvdt];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelay:1.0];
        [UIView setAnimationDuration:0.5];
        self.updateTimeLabel.frame = CGRectMake(0.0, -30.0, 320.0, 30.0);
        self.containerView.frame = CGRectMake(0.0, 0.0, 320.0, 416.0);
        [UIView commitAnimations]; 
    }
    if (!IsPaidVersion) {
        ADBannerView *banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 366.0, 320.0, 50.0)];
        banner.delegate = self;
        [self.view insertSubview:banner belowSubview:containerView];
        [banner release];
        NSLog(@"ADBannerView");
    }
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
    NSLog(@"bannerViewDidLoadAd");
    CGRect frame = containerView.frame;
    frame.size.height = frame.size.height - banner.bounds.size.height;
    //[UIView beginAnimations:nil context:nil];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    //[UIView setAnimationDuration:0.5];
    containerView.frame = frame;
    //[UIView commitAnimations];
    
    [self.view bringSubviewToFront:bannerView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.scrollView = nil;
    self.lastChildView = nil;
    self.leftOffsetImgaeView = nil;
    self.rightOffsetImageView = nil;
    self.selectedImageView = nil;
    self.titleView = nil;
    self.updateTimeLabel = nil;
    self.containerView = nil;
    self.bannerView.delegate=nil;
    self.bannerView = nil;
}

@end